<html>
<body>
<head><title>CS143 Project 1B by Shulin Jia</title></head>
<p>Created 4/21/2012 under distress.</p>
<p>
<form action="./query.php" method="GET">
<textarea name="query" cols="60" rows="8"></textarea>
<input type="submit" value="Submit" />
</form>
</p>


<?php
// set up:
$db_connection = mysql_connect("localhost", "cs143", "");
mysql_select_db("CS143", $db_connection);

//issuing a query
$query = $_GET["query"];
if ($query)
{
    
    $rs = mysql_query($query);
    if (!$rs)
    {
        die("Only SELECT and SHOW allowed");
    }
    $no_fields = mysql_num_fields($rs); // number of fields/columns
    echo '<table border="1">';
    echo "<tr>";
    
    // output column names
    for ( $i = 0; $i < $no_fields; $i++)
    {  
        echo "<td><b><center>";
        echo mysql_field_name($rs,$i);
        echo "</center></b></td>";
    }
    echo "</tr>";
    
    // output each row.
    while($row = mysql_fetch_row($rs)) {
        echo "<tr>";
        foreach ($row as $c)
        {
            if (is_null($c)) { echo "<td>N/A</td>"; }
            else { echo "<td>$c</td>"; }
        }
        echo "</tr>";
    }   
    
    echo '</table>';
}

mysql_close($db_connection);

?>
</body>
</html>